//
//  BMAAppManager.m
//  Autismus
//
//  Created by Nicolas CHENG on 04/12/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "BMAAppManager.h"
#import "BMAExercice.h"

/*
@implementation BMAEmotionObj : NSObject

@property (nonatomic, strong) NSString *keyName;
@property (nonatomic, strong) NSString *label;

@end

////////////////////////////////////////////////////////////////////////////////////

@interface BMAEmotionObj

@synthesize keyName;
@synthesize label;

@end

////////////////////////////////////////////////////////////////////////////////////
*/

@interface BMAAppManager ()

- (void)loadPlistIfNecessary;
- (NSString *)keyNameForEmotion:(BMAEmotion)aEmotion;
- (NSString *)labelForEmotion:(BMAEmotion)aEmotion;

@end

////////////////////////////////////////////////////////////////////////////////////

@implementation BMAAppManager

#pragma mark -
#pragma Singleton

+ (id)sharedInstance
{
    DEFINE_SHARED_INSTANCE_USING_BLOCK(^{
        return [[self alloc] init];
    });
}

#pragma mark -
#pragma Private

- (void)loadPlistIfNecessary
{
    if (settingsPlist == nil)
    {
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"];
        
        settingsPlist = [[NSDictionary alloc] initWithContentsOfFile:filePath];
    }
}

#pragma mark -
#pragma Public

- (NSDictionary *)emotions
{
    if (_emotions == nil)
    {
        [self loadPlistIfNecessary];
        
        NSDictionary *dictionary = [[settingsPlist objectForKey:@"root"] objectForKey:@"emotions"];
        
        _emotions = [NSDictionary dictionaryWithDictionary:dictionary];
    }
    
    return _emotions;    
}

- (NSArray *)exercices
{
    if (_exercices == nil)
    {
        [self loadPlistIfNecessary];
        
        NSDictionary *dictionary = [[settingsPlist objectForKey:@"root"] objectForKey:@"exercices"];
        
        NSUInteger count = [dictionary count];
        
        _exercices = [[NSMutableArray alloc] initWithCapacity:count];
        for (NSUInteger i = 0; i < count; i++)
        {
            [_exercices addObject:[NSNull null]];
        }
        
        NSString *name = nil;
        for (name in dictionary)
        {
            BMAExercice *exercice = [BMAExercice exerciceWithDictionary:[dictionary objectForKey:name] forName:name];
            
            if (exercice.index < count)
            {
                [_exercices replaceObjectAtIndex:exercice.index withObject:exercice];
            }
            else
            {
                DLOG(@"WARNING : Exercices dans Fichier PLIST mal configuré !!!");
            }
        }
    }
    
    return [NSArray arrayWithArray:_exercices];
}

- (BMAExercice *)exerciceForIndex:(NSUInteger)aIndex
{
    NSArray *exercices = [self exercices];
    
    BMAExercice *result = nil;
    
    if (aIndex < [exercices count])
    {
        result = [exercices objectAtIndex:aIndex];
    }
    else
    {
        DLOG(@"ERROR : Aucun exercice à l'index %i", aIndex);
    }
    
    return result;
}

- (UIImage *)randomPhoto
{
    [self loadPlistIfNecessary];
    
    NSUInteger count = [[self emotions] count];
    
    BMAEmotion emotion = RAND_INT_VAL(0, count - 1);
    
    return [self randomPhotoFor:emotion];
}

- (UIImage *)randomPhotoFor:(BMAEmotion)aEmotion
{
    [self loadPlistIfNecessary];
    
    NSString *keyName = [self keyNameForEmotion:aEmotion];
    
    NSArray *array = [[[settingsPlist objectForKey:@"root"] objectForKey:@"photos"] objectForKey:keyName];
    
    NSUInteger count = [array count];
    NSUInteger index = RAND_INT_VAL(0, count - 1);
    
    NSString *filename = [array objectAtIndex:index];
    
    UIImage *result = [UIImage imageNamed:filename];
    
    return result;
}

- (NSUInteger)photoCountFor:(BMAEmotion)aEmotion
{
    [self loadPlistIfNecessary];
    
    NSString *keyName = [self keyNameForEmotion:aEmotion];
    
    NSArray *array = [[[settingsPlist objectForKey:@"root"] objectForKey:@"photos"] objectForKey:keyName];
    
    NSUInteger result = [array count];
    
    return result;
}

- (NSString *)randomPhotoPathFor:(BMAEmotion)aEmotion
{
    [self loadPlistIfNecessary];
    
    NSString *keyName = [self keyNameForEmotion:aEmotion];
    
    NSArray *array = [[[settingsPlist objectForKey:@"root"] objectForKey:@"photos"] objectForKey:keyName];
    
    NSUInteger count = [array count];
    NSUInteger index = RAND_INT_VAL(0, count - 1);
    
    NSString *filename = [array objectAtIndex:index];
    
    return filename;
}

- (UIImage *)illustrationFor:(BMAEmotion)aEmotion
{
    [self loadPlistIfNecessary];
    
    NSString *keyName = [self keyNameForEmotion:aEmotion];
    
    NSArray *array = [[[settingsPlist objectForKey:@"root"] objectForKey:@"illustrations"] objectForKey:keyName];
    
    // pour le moment il n'y a qu'une seule illustration
    // par émotion
    NSString *filename = [array objectAtIndex:0];
    
    UIImage *result = [UIImage imageNamed:filename];
    
    return result;
}

- (NSString *)randomIllustrationPathFor:(BMAEmotion)aEmotion
{
    return nil;
}

- (NSString *)keyNameForEmotion:(BMAEmotion)aEmotion
{
    NSString *result = nil;
    
    if (aEmotion == BMAEmotionEnColere)
    {
        result = @"colere";
    }
    else if (aEmotion == BMAEmotionJoyeux)
    {
        result = @"joyeux";
    }
    else if (aEmotion == BMAEmotionTriste)
    {
        result = @"triste";
    }
    else if (aEmotion == BMAEmotionMalade)
    {
        result = @"malade";
    }
    else if (aEmotion == BMAEmotionEtonne)
    {
    result = @"etonne";
    }
    else if (aEmotion == BMAEmotionEffraye)
    {
        result = @"effraye";
    }
    
    return result;
}

- (NSString *)labelForEmotion:(BMAEmotion)aEmotion
{
    NSString *keyName = [self keyNameForEmotion:aEmotion];
    
    return [[self emotions] objectForKey:keyName];
}

@end
